package com.example.asus.yuejian_10.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.example.asus.yuejian_10.R;
import com.example.asus.yuejian_10.base.BaseFragment;


/**
 * A simple {@link Fragment} subclass.
 */
public class MainFragment extends BaseFragment {
    private RadioGroup radioGroup;

    private RecommendFragment recommendFragment;
    private ClubFragment clubFragment;
    private HomeFragment homeFragment;
    private FindFragment findFragment;
    private MeFragment meFragment;

    private FragmentManager manager;
    private FragmentTransaction transaction;


    public MainFragment() {
        // Required empty public constructor
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup);
        manager = getChildFragmentManager();

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {

                switch (i){
                    case R.id.radio_recommend:
                        recommendFragment = new RecommendFragment();
                        replace(recommendFragment);
                        break;
                    case R.id.radio_club:
                        clubFragment = new ClubFragment();
                        replace(clubFragment);
                        break;
                    case R.id.radio_home:
                        homeFragment = new HomeFragment();
                        replace(homeFragment);
                        break;
                    case R.id.radio_find:
                        findFragment = new FindFragment();
                        replace(findFragment);
                        break;
                    case R.id.radio_me:
                        meFragment = new MeFragment();
                        replace(meFragment);
                        break;
                }
            }
        });
        radioGroup.check(R.id.radio_home);



    }


    private void replace(Fragment fragment){
        transaction = manager.beginTransaction();
        transaction.replace(R.id.frameLayout,fragment);
        transaction.commit();
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_main, container, false);
    }


}
